import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../shared/services/baseHttp.service";
import { Observable } from "rxjs";
import { HttpClient, HttpParams } from "@angular/common/http";
import { AccountModel } from "./models/accounts.model";
import { UserModel, StatusEnum } from "../models/user.model";

@Injectable()
export class AccountsService extends BaseHttpService {
    constructor(private http: HttpClient) {
        super()
    }

    public getLastCRMSyncDate(): Observable<string> {
        let params = new HttpParams();
        params = params.set('returnLastCrmSyncDate', true.toString());
        return this.http.get<string>(this.serverUrl + '/accountsserv/api/v1/accounts/', { params: params });
    }

    public getAccountsList(term: string, status: string, skip: number = 0, take: number = 20): Observable<AccountModel[]> {
        let params = new HttpParams();
        params = params.set('name', term)
            .set('returnAccounts', true.toString())
            .set('status', status)
            .set('orderAscending', true.toString())//column sort not yet implemented
            .set('orderBy', 'name')
            .set('skip', skip.toString())
            .set('take', take.toString());
        return this.http.get<AccountModel[]>(this.serverUrl + '/accountsserv/api/v1/accounts/', { params: params });
    }

    public getAccountsCount(term: string, status: string): Observable<number> {
        let params = new HttpParams();
        params = params.set('getCount', true.toString())
            .set('name', term)
            .set('status',  status);
        return this.http.get<number>(this.serverUrl + '/accountsserv/api/v1/accounts/', { params: params });
    }

    public getAccount(accountId: string): Observable<AccountModel> {
        return this.http.get<AccountModel>(this.serverUrl + '/accountsserv/api/v1/accounts/' + accountId);
    }

    public getAccountUserList(accountId: string, status: StatusEnum, skip: number = 0, take: number = 20): Observable<UserModel[]> {
        let params = new HttpParams();
        params = params.set('status', StatusEnum[status])
            .set('skip', skip.toString())
            .set('take', take.toString());
        return this.http.get<UserModel[]>(this.serverUrl + '/accountsserv/api/v1/accounts/' + accountId + '/users', { params: params });
    }

    public getAccountUserCount(accountId: string, status: StatusEnum): Observable<number> {
        let params = new HttpParams();
        params = params.set('getCount', true.toString())
            .set('status', status.toString());
        return this.http.get<number>(this.serverUrl + '/accountsserv/api/v1/accounts/' + accountId + '/users', { params: params });
    }

    public resetPass(users: string[]): Observable<void> {
        return this.http.patch<void>(this.serverUrl + '/accountsserv/api/v1/users/', [{ resourceIds: users, verb: 'RESETPASSWORD' }]);
    }
}